package com.dmap.run.controller.api;

import com.dmap.base.constants.Constant;
import com.dmap.base.vo.BaseVo;
import com.dmap.run.bo.SwimRecordSimple;
import com.dmap.run.constants.RunConstans;
import com.dmap.run.controller.api.param.SwimRecordParam;
import com.dmap.run.po.SwimRecord;
import com.dmap.run.service.MapTrackService;
import com.dmap.run.service.SwimRecordService;
import com.dmap.run.vo.SwimRecordHistoryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * \* User: silent
 * \* Date: 08/06/18 Time: 上午 10:39
 * \* Description:
 * \
 */
@Api(value = "游泳相关接口", description = "游泳相关接口")
@Controller
@RequestMapping(value = "/api/dmap-run-server/v1/swim")
public class SwimRecordController {

    @Resource
    private SwimRecordService swimRecordService;

    @ResponseBody
    @ApiOperation(value = "接受游泳数据", notes = "接受游泳数据")
    @RequestMapping(value = "/accept/swim/data", method = RequestMethod.POST)
    public BaseVo accpectRunRecord(@RequestBody SwimRecordParam param) {
        BaseVo baseVo = new BaseVo();
        if (param.getUserId() == null
                || param.getDataSource() == null
                || param.getStartTime() == null
                || param.getEndTime() == null
                || param.getCalorie() == null
                || param.getStrokeNumber() == null
                || param.getStrokeRate() == null) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }
        if (swimRecordService.isExistSwimRecord(param.getUserId(), param.getStartTime(), param.getEndTime())) {
            baseVo.setCode(RunConstans.RESPONSE_IS_EXIST_THIS_RECORD_CODE);
            baseVo.setMsg(RunConstans.RESPONSE_IS_EXIST_THIS_RECORD_MSG);
            return baseVo;
        }
        boolean isSuccess = swimRecordService.insertSwimRecord(param);
        if (!isSuccess) {
            baseVo.setCode(Constant.RESPONSE_INSERT_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_INSERT_ERROR_MSG);
            return baseVo;
        }
        return baseVo;
    }

    @ResponseBody
    @ApiOperation(value = "获取用户历史游泳总时长", notes = "获取用户历史游泳总时长")
    @RequestMapping(value = "/get/user/total/time", method = RequestMethod.GET)
    public BaseVo getUserTimeTotal(@RequestParam Long userId) {
        BaseVo baseVo = new BaseVo();
        if (userId == null) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }
        int userTotalTime = swimRecordService.selectUserTotalTime(userId);
        Map<String, Integer> map = new HashMap<>();
        map.put("timeTotal", userTotalTime);
        baseVo.setData(map);
        return baseVo;
    }


    @ResponseBody
    @ApiOperation(value = "获取历史游泳数据列表", notes = "获取历史游泳数据列表")
    @RequestMapping(value = "/get/swim/data", method = RequestMethod.GET)
    public BaseVo getSwimRecordList(@RequestParam Long userId, @RequestParam Integer pageIndex, @RequestParam Integer pageSize) {
        BaseVo baseVo = new BaseVo();
        if (userId == null || pageIndex == null || pageSize == null) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }
        SwimRecordHistoryVo swimRecordHistoryVo = swimRecordService.selectSwimRecordHistoryList(userId);
        if (swimRecordHistoryVo.getSwimNum() > 0) {
            List<SwimRecord> runRecordList = swimRecordService.selectSwimRecordListByPage(userId, pageIndex, pageSize);
            List<SwimRecordSimple> swimRecordSimpleList = new ArrayList<>(pageSize);
            for (SwimRecord record : runRecordList) {
                swimRecordSimpleList.add(new SwimRecordSimple(record));
            }
            swimRecordHistoryVo.setSwimRecordSimpleList(swimRecordSimpleList);
        }
        baseVo.setData(swimRecordHistoryVo);
        return baseVo;
    }
}
